"""
菜单权限相关模型
"""

from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Text
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func

from ..core.database import Base


class MenuPermission(Base):
    """菜单权限表"""
    __tablename__ = "menu_permissions"

    id = Column(Integer, primary_key=True, index=True)
    menu_key = Column(String(50), unique=True, nullable=False, comment="菜单标识")
    menu_name = Column(String(100), nullable=False, comment="菜单名称")
    menu_path = Column(String(200), nullable=False, comment="菜单路径")
    parent_id = Column(Integer, ForeignKey("menu_permissions.id"), comment="父菜单ID")
    icon = Column(String(50), comment="菜单图标")
    sort_order = Column(Integer, default=0, comment="排序顺序")
    required_role = Column(String(20), default="user", comment="所需角色：user/admin")
    required_permissions = Column(Text, comment="所需权限")
    is_active = Column(Boolean, default=True, comment="是否激活")
    is_visible = Column(Boolean, default=True, comment="是否可见")
    description = Column(Text, comment="菜单描述")
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # 关系
    user_menu_permissions = relationship("UserMenuPermission", back_populates="menu")

    def __repr__(self):
        return f"<MenuPermission(key='{self.menu_key}', name='{self.menu_name}')>"


class UserMenuPermission(Base):
    """用户菜单权限表"""
    __tablename__ = "user_menu_permissions"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
    menu_id = Column(Integer, ForeignKey("menu_permissions.id", ondelete="CASCADE"), nullable=False)
    is_granted = Column(Boolean, nullable=False, default=True, comment="是否授权访问")
    granted_by = Column(Integer, ForeignKey("users.id"), nullable=False, comment="授权人")
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # 关系
    user = relationship("User", foreign_keys=[user_id])
    menu = relationship("MenuPermission", back_populates="user_menu_permissions")
    granter = relationship("User", foreign_keys=[granted_by])

    def __repr__(self):
        return f"<UserMenuPermission(user_id={self.user_id}, menu_id={self.menu_id}, is_granted={self.is_granted})>"


class RolePermission(Base):
    """角色权限表"""
    __tablename__ = "role_permissions"

    id = Column(Integer, primary_key=True, index=True)
    role_name = Column(String(50), nullable=False, comment="角色名称")
    menu_id = Column(Integer, ForeignKey("menu_permissions.id", ondelete="CASCADE"), nullable=False)
    is_granted = Column(Boolean, default=True, comment="是否授权")
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    def __repr__(self):
        return f"<RolePermission(role='{self.role_name}', menu_id={self.menu_id})>"


class UserRole(Base):
    """用户角色表"""
    __tablename__ = "user_roles"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
    role_name = Column(String(50), nullable=False, comment="角色名称")
    assigned_by = Column(Integer, ForeignKey("users.id"), nullable=False, comment="分配人")
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # 关系
    user = relationship("User", foreign_keys=[user_id])
    assigner = relationship("User", foreign_keys=[assigned_by])

    def __repr__(self):
        return f"<UserRole(user_id={self.user_id}, role='{self.role_name}')>"